package cn.signalpha.modules.social.controller.api;

import cn.signalpha.common.constant.AppConstant;
import cn.signalpha.common.constant.MsgConstant;
import cn.signalpha.common.dto.IdDTO;
import cn.signalpha.common.utils.Assert;
import cn.signalpha.core.boot.ctrl.BaseController;
import cn.signalpha.core.mp.support.Condition;
import cn.signalpha.core.mp.support.Query;
import cn.signalpha.core.tool.api.R;
import cn.signalpha.modules.social.dto.CommentDTO;
import cn.signalpha.modules.social.entity.Comment;
import cn.signalpha.modules.social.service.CommentService;
import cn.signalpha.modules.social.vo.CommentVO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.Map;

/**
 * 控制器
 *
 * @author signalpha
 */
@RestController
@AllArgsConstructor
@RequestMapping(AppConstant.APPLICATION_SOCIAL_NAME + "/wap/comment")
@Api(value = "评论", tags = "评论")
public class ApiCommentController extends BaseController {

	private final CommentService commentService;

    @GetMapping("/page")
	@ApiImplicitParams({
		@ApiImplicitParam(name="targetId", dataType = "string", value = "主题id"),
	})
    @ApiOperation(value = "评论列表", notes = "")
    public R<IPage<CommentVO>> page(@ApiIgnore @RequestParam Map<String, Object> params, Query query) {
		params.put("status", "1");
		params.put("isShow", "1");
		params.put("userId", getUser().getUserId().toString());
		IPage<CommentVO> page = commentService.selectCommentPage(Condition.getPage(query), params);

		Map<String, Object> replyParam = new HashMap<>(3);
		replyParam.put("status", "1");
		replyParam.put("isShow", "1");
		replyParam.put("userId", params.get("userId").toString());
		Query replyQuery = new Query().setCurrent(1).setSize(500);
		page.getRecords().forEach(commentVO -> {
			if(commentVO.getReplyNum() > 0){
				replyParam.put("parentId", commentVO.getId().toString());
				commentVO.setReplyList(commentService.selectCommentReplyPage(Condition.getPage(replyQuery), replyParam));
			}
		});
        return R.data(page);
    }

	@GetMapping("/reply/page")
	@ApiImplicitParams({
		@ApiImplicitParam(name="parentId", dataType = "string", value = "评论id"),
	})
	@ApiOperation(value = "回复列表", notes = "")
	public R<IPage<CommentVO>> replyPage(@ApiIgnore @RequestParam Map<String, Object> params, Query query) {
		params.put("status", "1");
		params.put("isShow", "1");
		params.put("userId", getUser().getUserId().toString());
		return R.data(commentService.selectCommentReplyPage(Condition.getPage(query), params));
	}

	@PostMapping("/save")
	@ApiOperation(value = "发表评论", notes = "")
	public R<Comment> save(@Valid @RequestBody CommentDTO commentDTO) {
//		commentDTO.setCity(IpToCityUtil.query(WebUtil.getIP()));
		if(commentDTO.getTargetType() == 1){
			return R.data(commentService.saveCommentTopic(commentDTO, getUser().getUserId()));
		}else{
			return R.data(commentService.saveCommentReply(commentDTO, getUser().getUserId()));
		}
	}

	@GetMapping("/my/page")
	@ApiOperation(value = "我的评论", notes = "")
	public R<IPage<CommentVO>> myPage(@ApiIgnore @RequestParam Map<String, Object> params, Query query) {
		params.put("fromUserId", getUser().getUserId().toString());
		return R.data(commentService.selectCommentPage(Condition.getPage(query), params));
	}

	@PostMapping("/my/update")
	@ApiOperation(value = "我的评论 编辑", notes = "")
	public R remove(@Valid @RequestBody CommentDTO.ReplyTO replyTO) {
		Comment comment = commentService.getById(replyTO.getId());
		Assert.isNull(comment, MsgConstant.OBJECT_NULL);
		Assert.isTrue(comment.getFromUserId().equals(getUser().getUserId()), MsgConstant.UNAUTHORIZED);
		Assert.isFalse(comment.getStatus() == 1, "当前状态不可编辑");
		return R.status(commentService.updateById(Comment.builder().id(comment.getId()).content(replyTO.getContent()).extend(replyTO.getExtend()).build()));
	}

	@PostMapping("/my/remove")
	@ApiOperation(value = "我的评论 删除", notes = "")
	public R remove(@Valid @RequestBody IdDTO idDTO) {
		Comment comment = commentService.getById(idDTO.getId());
		Assert.isNull(comment, MsgConstant.OBJECT_NULL);
		Assert.isTrue(comment.getFromUserId().equals(getUser().getUserId()), MsgConstant.UNAUTHORIZED);
		return R.status(commentService.removeById(idDTO.getId()));
	}

}
